package com.ytjj.qmyx.mall.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.response.QueryTrackResp;
import com.kuaidi100.sdk.utils.PropertiesReader;
import com.kuaidi100.sdk.utils.SignUtils;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.CopyWriterConstant;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.CopyWriterConfigUtil;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.model.response.OldSysData;
import com.ytjj.qmyx.mall.model.response.UserOrderResponse;
import com.ytjj.qmyx.mall.service.OldSysService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/old")
@RestController
@Slf4j
public class OldSysController {

    @Autowired
    private OldSysService oldSysService;
    @Autowired
    private RedisService redisService;
    @Resource
    private CopyWriterConfigUtil copyWriterConfigUtil;
    //白剑域名查询订单
    @Value("${baijian.queryOrder.url:https://XXX.baijkj.com}")
    private String baijianQueryOrderUrl;
    @Value("${yiquan.queryOrder.url:https://XXX.yiquan-keji.com}")
    private String yiquanQueryOrderUrl;

    /**
     * 根据手机号查询订单
     * @param phone 手机号
     * @return CommonResult
     */
//    @RequestMapping("/sys")
//    public CommonResult visitOldSysByPhone(@RequestParam("phone") String phone) {
//        if(null == phone || "".equals(phone)){
//            return CommonResult.failed("电话号码不能为空");
//        }
//        List<UserOrderResponse> list = oldSysService.selectList(phone);
//        OldSysData oldSysData = new OldSysData();
//        oldSysData.setUserOrder(list);
//        return CommonResult.success(oldSysData);
//    }

    @RequestMapping("/appSys")
    public CommonResult appVisitOldSysByPhone(@RequestParam("phone") String phone, @RequestParam("authCode") String authCode) {
        if (StringUtils.isEmpty(authCode)) {
            return CommonResult.failed("验证码不能为空");
        }
        if (StringUtils.isBlank(phone)){
            return CommonResult.failed("手机号不能为空");
        }
        phone = phone.trim();
        authCode = authCode.trim();

        //首先比对验证码是否失效
        String redisAuthCode = redisService.get(RedisConstants.REDIS_KEY_PREFIX_AUTH_CODE_LOGINPHONE + phone);
        if (StringUtils.isEmpty(redisAuthCode)) {
            //如果未取到则过期
            return CommonResult.success(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.VERIFICATION_OUT));
        }
        if (!"".equals(redisAuthCode) && !authCode.equals(redisAuthCode)) {
            return CommonResult.success(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.VERIFICATION_CODE_ERROR));
        }
        List<UserOrderResponse> list = oldSysService.selectList(phone);
        OldSysData oldSysData = new OldSysData();
        oldSysData.setUserOrder(list);
        return CommonResult.success(oldSysData);
    }


    CommonResult   queryBaijianOrderByPhone(String phone) {
        String baijianUrl = baijianQueryOrderUrl + "/mall/old/innerQueryOrderByPhone?phone=" + phone;
        String baijianResultJson = HttpClientUtil.doGet(baijianUrl);
        return JSONObject.parseObject(baijianResultJson, CommonResult.class);
    }
    CommonResult queryYiQuanOrderByPhone(String phone) {
        String yiquanUrl = yiquanQueryOrderUrl + "/mall/old/innerQueryOrderByPhone?phone=" + phone;
        String yiquanResultJson = HttpClientUtil.doGet(yiquanUrl);
        return JSONObject.parseObject(yiquanResultJson, CommonResult.class);
    }


    @GetMapping("/innerQueryOrderByPhone")
    public CommonResult innerQueryOrderByPhone(@RequestParam("phone") String phone) {
        List<UserOrderResponse> list = oldSysService.selectList(phone);
        OldSysData oldSysData = new OldSysData();
        oldSysData.setUserOrder(list);
        return CommonResult.success(oldSysData);
    }


//    @RequestMapping("/getExpress")
//    public CommonResult getExpress(@RequestParam("com") String com,
//                                   @RequestParam("num") String num,
//                                   @RequestParam("phone") String phone) {
//        if(null == num || "".equals(num)){
//            return CommonResult.failed("");
//        }
//        QueryTrack queryTrack = new QueryTrack();
//        QueryTrackReq queryTrackReq = new QueryTrackReq();
//        QueryTrackParam queryTrackParam = new QueryTrackParam();
//        queryTrackParam.setCom(com);
//        queryTrackParam.setNum(num);
//        queryTrackParam.setPhone(phone);
//        queryTrackReq.setParam(queryTrackParam);
//        queryTrackReq.setCustomer(PropertiesReader.get("customer"));
//        queryTrackReq.setSign(SignUtils.sign(new Gson().toJson(queryTrackParam) + PropertiesReader.get("key") + PropertiesReader.get("customer")));
//        QueryTrackResp resp = queryTrack.queryTrack(queryTrackReq);
////        List<QueryTrackData> respData = resp.getData();
//        log.info("获取快递信息响应：info:{}", JSONObject.toJSONString(resp));
//        return CommonResult.success(resp);
//    }


}
